package com.fancy.learncenter.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.TextView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.CartoonTurnipBillBoardItemBean;
import com.fancy.learncenter.bean.CartoonTurnipResultBean;
import com.fancy.learncenter.common.MyApplication;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.adapter.TurnipBillBoardAdapter;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Response;

public class TurnipBillBoardActivity extends BaseActivity {

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.my_turnips)
    TextView myTurnips;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    private List<CartoonTurnipBillBoardItemBean> list;
    private TurnipBillBoardAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_turnip_bill_board, Status.STATUS);
        ButterKnife.bind(this);
        initView();
        initData();
    }


    private void initView() {
        list = new ArrayList<>();
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        adapter = new TurnipBillBoardAdapter(this, list);
        recyclerView.setAdapter(adapter);

    }

    private void initData() {

        Map<String, Object> map = new HashMap<>();
        map.put("classId", MyApplication.classIDStr);
        HttpMehtod.getInstance().cartoonListRadishRank(map, new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {

                BaseBean<CartoonTurnipResultBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CartoonTurnipResultBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<CartoonTurnipResultBean>() {
                    @Override
                    public void success(CartoonTurnipResultBean dataBean) {

                        if (dataBean != null) {
                            int turnipNum = Double.valueOf(dataBean.getResult().getMyselfRadishCount()).intValue();
                            myTurnips.setText("我的桃子：" + turnipNum);
                            list.addAll(dataBean.getResult().getStudentRadishVOS());
                            adapter.notifyDataSetChanged();
                        }

                    }

                    @Override
                    public void fail(String errorCode) {

                    }
                }).dealData();

            }
        });

    }


    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}
